﻿@{
    ViewBag.Title = "员工基本信息";
    Layout = "~/Views/Shared/InfoMaste.cshtml";
}


<script language="javascript" type="text/javascript">
    //搜索员工信息信息
    function doSearch(depId, levelId) {
        $('#dg').datagrid('load', {
            empCard: $('#empCard').textbox('getValue'),
            empName: $('#empName').textbox('getValue'),
            empMobile: $('#empMobile').textbox('getValue'),
            empNum: $('#empNum').textbox('getValue'),
            depId: depId,
            levelId: levelId
        });
    }
    //初始化公司
    function InitCompany() {
        $('#CompanyId').combobox({
            url: '/BasicManage/GetLKCompany',
            valueField: 'Id',
            textField: 'CompanyName',
            method: "post",
            required: true
        });
    }

    //初始化部门
    function InitRole() {
        $('#RoleId').combobox({
            url: '/SystemSet/GetRoleInfJson',
            valueField: 'RoleId',
            textField: 'RoleName',
            method: "post",
            required: true
        });
    }

    //初始化部门
    function InitDep(depId) {
        $.get("/EmployeeInfo/GetDepComBoTreeJson", function (json) {
            $('#DepId').combotree('loadData', json);
            if (depId != undefined) {
                $('#DepId').combotree('setValue', depId);
            }
        });
    }
    //初始化岗位
    function InitLevel(depId) {
        if (depId == undefined) return false;
        $('#LevelId').combobox({
            url: '/EmployeeInfo/GetLevelComboBoxJson?depId=' + depId,
            valueField: 'LevelId',
            textField: 'LevelName',
            method: "post",
            required: true
        });
    }


    

    //添加员工基本信息
    function addEmployeeInfo() {
        $('#fm').form('clear');
        InitDep();
        InitRole();
        InitCompany();
        $('#EmpSex').combobox('setValue', 1);
        $("#EmployeeId").val(0);
        $('#dlg').dialog('open').dialog('setTitle', '新增');
        url = '/EmployeeInfo/AddEmployee';
    }

    //编辑员工基本信息和岗位信息
    function editEmployeeInfo() {
        var row = $('#dg').datagrid('getSelected');
        if (!row) {
            Helper.Show("请选择数据在编辑!");
            return;
        }
        $('#fm').form('clear');
        InitDep(row.DepId);
        InitLevel(row.DepId);
        InitRole();
        InitCompany();
        $('#dlg').dialog('open').dialog('setTitle', '编辑');
        $.messager.progress({
            title: '请稍后',
            msg: '数据加载中...',
        });
        window.setTimeout(function () {
            $('#fm').form('load', row);
            $.messager.progress('close');
        }, 1000);
        url = '/EmployeeInfo/EditEmployee';
    }

    function formatterSex(value, rowData, rowIndex) {
        return value == 1 ? "男" : "女";
    }


    function formatter_EmployeeId(value, rowData, rowIndex) {
        ahtml = '<a  href="javascript:" onclick="restEmployee(' + rowData.EmployeeId + ')"  style="cursor: pointer; color:Black;padding-left: 10px;">重置密码</a>';
        return ahtml;
    }

    //删除员工
    function delEmployee() {
        var row = $('#dg').datagrid('getSelected');
        if (!row) {
            Helper.Show("请选择数据在删除!");
            return;
        }
        $.messager.confirm('提示信息', '确认要删除吗?', function (r) {
            if (!r) {
                return false;
            }
            $.post("/EmployeeInfo/DelEmployee", { id: row.EmployeeId }, function (json) {
                Helper.Show(json.Msg);
                if (json.Success) {
                    doSearch();
                }
            });
        });

    }

    //重置密码
    function restEmployee(employeeId) {
        $.messager.confirm('提示信息', '确认重置密码吗?', function (r) {
            if (r) {
                $.post("/EmployeeInfo/RestPassWord", { employeeId: employeeId }, function (json) {
                    Helper.Show(json.Msg);
                    if (json.Success) {
                        doSearch();
                    }
                });
            }
        });
    }



    function TreeOnclickRow(note) {
        if (note.attributes == null) {
            doSearch(note.id);
        }
        else {
            doSearch(null, note.id);
        }
    }

    function saveEmployeeInfo() {
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (json) {
                var json = eval('(' + json + ')');
                Helper.Show(json.Msg);
                if (json.Success) {
                    $('#fm').form('clear');
                    $('#dlg').dialog('close');
                    $('#dg').datagrid('reload');
                }
            }
        });
    }


    function authWareHouse() {
        var row = $('#dg').datagrid('getSelected');
        if (!row) {
            Helper.Show("请选择员工在操作!");
            return;
        }
        $('#dlg_WareHouse').dialog('open');
        $('#fm_WareHouse').form('clear');
        $('#fm_WareHouse').form('load', row);
        InitWareHouse(row.EmployeeId);
    }

    function InitWareHouse(empId) {
        $('#WareHouseId').combobox({
            valueField: 'Id',
            textField: 'WareHouseName',
            url: '/LKWareHouse/GetLKWareHousesTopList',
            editable: false,
            multiple: true,
            method: 'post',
            onLoadSuccess: function (json) {
                $.post('/EmployeeInfo/GetUserWareHouseList', { EmployeeId: empId }, function (data) {
                    $('#WareHouseId').combobox("setValues", data);
                });
            }
        });
    }


    function saveAuthWareHouse() {
        var EmployeeId = $('#wareHouse_EmployeeId').val();
        var WareHouseId = $('#WareHouseId').combobox('getValues');
        $.post("/EmployeeInfo/AddUserWareHouse", { WareHouseId: WareHouseId,EmployeeId: EmployeeId }, function (json) {
            Helper.Show(json.Msg);
            if (json.Success) {
                $('#dlg_WareHouse').dialog('close');
                doSearch();
            }
        });
    }
</script>
<div class="easyui-panel" title="员工基本" style="width: auto; padding: 10px;" fit="true">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:true" style="width: 180px; padding: 10px">
            <ul id="tt" class="easyui-tree" data-options="url:'/BasicManage/InitDepLevelTree',method:'post',animate:true,lines:true,onClick: function(node){TreeOnclickRow(node);}"></ul>
        </div>
        <div data-options="region:'center'" style="padding: 10px;width:800px;">
            <table id="dg" class="easyui-datagrid"
                   url="/EmployeeInfo/GetEmployeeList" toolbar="#tb" title="列表"
                   data-options="
                   fit:true,
                   pagination:true,
                   pagelist:[10],
                   fitcolumns:true,
                   singleSelect: true,
                   collapsible: true,
                   rownumbers: true">
                <thead data-options="frozen:true">
                    <tr>
                        <th field="EmployeeId" formatter="formatter_EmployeeId" width="120">操作</th>
                        <th field="EmpNum" width="80">
                            工号
                        </th>
                        <th field="EmpName" width="80">
                            姓名
                        </th>
                    </tr>
                </thead>
                <thead>
                    <tr>
                        <th field="EmpCard" align="right" width="150">
                            身份证
                        </th>
                        <th field="EmpMobile" align="right" width="150">
                            移动电话
                        </th>
                        <th field="DepName" align="right" width="120">
                            所属部门
                        </th>
                        <th field="LevelName" align="right" width="120">
                            所属岗位
                        </th>
                        <th field="RoleName" align="right" width="120">
                            所属角色
                        </th>
                        <th field="CompanyName" align="right" width="200">
                            所属公司
                        </th>
                    </tr>
                </thead>
            </table>
            <div id="tb" style="padding: 5px; height: auto;">
                <span>工号</span>
                <input id="empNum" class="easyui-textbox" style="width: 173px; height: 30px;" />
                <span>移动电话</span>
                <input id="empMobile" class="easyui-textbox" style="width: 173px; height: 30px;" />
                <span>身份证号码</span>
                <input id="empCard" class="easyui-textbox" style="width: 173px; height: 30px;" />
                <span>姓名</span>
                <input id="empName" class="easyui-textbox" style="width: 173px; height: 30px;" />
                <div>
                    <a href="javascript:" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addEmployeeInfo()">添加</a>
                    <a href="javascript:" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editEmployeeInfo()">编辑</a>
                    <a href="javascript:" class="easyui-linkbutton" iconcls="icon-cancel" plain="true" onclick="delEmployee()">删除</a>
                    @*<a href="javascript:" class="easyui-linkbutton" plain="true" iconcls="icon-filter" onclick="authWareHouse()">冷库授权</a>*@
                    <a href="javascript:" class="easyui-linkbutton" plain="true" iconcls="icon-search" onclick="doSearch()">查询</a>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="dlg" class="easyui-dialog" title="新增" data-options="iconCls:'icon-save',buttons:'#dlg-buttons'" closed="true" style="width: 750px; height: 480px;" modal="true">
    <form id="fm" method="post" novalidate>
        <input name="CityId" type="hidden" />
        <input name="EmployeeId" id="EmployeeId" type="hidden" value="0" />
        <div class="fitem1">
            <label>
                所属公司 :
            </label>
            <input name="CompanyId" id="CompanyId" class="easyui-combobox" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                工号:
            </label>
            <input name="EmpNum" class="easyui-textbox" data-options="validType:'length[1,6]',prompt:'6位数字'" autocomplete="off" style="width: 173px; height: 30px;" />
            <label>
                所属角色 :
            </label>
            <input name="RoleId" id="RoleId" class="easyui-combobox" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                姓名:
            </label>
            <input name="EmpName" class="easyui-textbox" data-options="required:true,validType:'length[1,10]'" autocomplete="off" style="width: 173px; height: 30px;" />
            <label>
                电话号码:
            </label>
            <input name="EmpMobile" class="easyui-textbox" data-options="required:true,validType:'length[1,11]'" autocomplete="off" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                所属部门:
            </label>
            <input name="DepId" id="DepId" class="easyui-combotree" style="width: 173px; height: 30px;" data-options="required:true, onChange: function (newValue, oldValue) {InitLevel(newValue)}" />
            <label>
                所属岗位 :
            </label>
            <input name="LevelId" id="LevelId" class="easyui-combobox" data-options="required:true,editable: false" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                性别:
            </label>
            <select name="EmpSex" id="EmpSex" class="easyui-combobox" data-options="editable:false" style="width: 173px; height: 30px;">
                <option value="1" selected="selected">男</option>
                <option value="2">女</option>
            </select>
        </div>
        <div class="fitem1">
            <label>
                身份证:
            </label>
            <input name="EmpCard" maxlength="18" style="width: 173px; height: 30px;" onblur="cardVaild(this.value)" autocomplete="off" />
            <script type="text/javascript">
                function cardVaild(value) {
                    if (value == "") {
                        return false;
                    }
                    var re = /^\d{6}(((19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}([0-9]|x|X))|(\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}))$/;
                    if (!re.test(value)) {
                        Helper.Show('请填写正确的身份证信息!');
                        return false;
                    }
                    var empId = $("#EmployeeId").val();
                    $.post("/EmployeeInfo/IsEmployeeInfo_EmpCard", { empId: empId, empCard: value }, function (json) {
                        if (json.Success) {
                            Helper.Show(json.Msg);
                            return false;
                        }
                    });
                    //获取生日
                    $("#EmpBirthday").datebox("setValue", value.substring(6, 10) + "-" + value.substring(10, 12) + "-" + value.substring(12, 14));
                    //获取性别
                    $("#EmpSex").combobox('setValue', parseInt(value.substr(16, 1)) % 2 == 0 ? 2 : 1);

                    //获取年龄
                    var myDate = new Date();
                    var month = myDate.getMonth() + 1;
                    var day = myDate.getDate();
                    var age = myDate.getFullYear() - value.substring(6, 10) - 1;
                    if (value.substring(10, 12) < month || value.substring(10, 12) == month && value.substring(12, 14) <= day) {
                        age++;
                    }
                    $("#EmpAge").numberbox("setValue", age);
                }
            </script>
        </div>
        <div class="fitem1">
            <label>
                年龄:
            </label>
            <input name="EmpAge" id="EmpAge" class="easyui-numberbox" data-options="validType:'length[1,2]'" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                生日:
            </label>
            <input name="EmpBirthday" id="EmpBirthday" class="easyui-datebox" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                性别:
            </label>
            <select name="EmpSex" id="EmpSex" class="easyui-combobox" data-options="editable:false" style="width: 173px; height: 30px;">
                <option value="1" selected="selected">男</option>
                <option value="2">女</option>
            </select>
        </div>
        <div class="fitem1">
            <label>
                员工说明:
            </label>
            <textarea rows="4" cols="4" style="width: 450px; height: 90px; vertical-align: top" name="Caption" autocomplete="off" placeholder="500字以内"></textarea>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveEmployeeInfo()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>

@*<div id="dlg_WareHouse" class="easyui-dialog" title="冷库授权" data-options="iconCls:'icon-save',buttons:'#dlg-buttons_WareHouse'" closed="true" style="width: 600px; height: 150px;" modal="true">
    <form id="fm_WareHouse" style="padding-top:10px;">
        <input name="EmployeeId" type="hidden" id="wareHouse_EmployeeId" />
        <div class="fitem1">
            <label>
                仓库选择:
            </label>
            <input class="easyui-combobox" id="WareHouseId"  name="WareHouseId" style="width: 400px;">
        </div>
    </form>
</div>
<div id="dlg-buttons_WareHouse">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveAuthWareHouse()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg_WareHouse').dialog('close')">取消</a>
</div>*@


